package com.edu.exam.entity.curriculum.structure;

import com.edu.exam.entity.curriculum.course.Course;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 知识点实体
 * 所属模块：B-01-02 课程结构管理子模块
 * 对应设计文档：B-01-02-06-数据库设计.md
 *
 * 管理课程的基础知识点信息，知识点归属于具体的章节
 * 为题库建设提供基本的结构化数据支持，支持知识点分类管理
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Table(name = "B01_02_KNOWLEDGE_POINT",
       uniqueConstraints = @UniqueConstraint(columnNames = {"code"}))
@Comment("课程知识点表")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class KnowledgePoint {

    /**
     * 知识点ID，主键
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 知识点编码，唯一标识符
     */
    @NotBlank(message = "知识点编码不能为空")
    @Column(name = "code", nullable = false, unique = true, length = 50)
    private String code;

    /**
     * 知识点名称
     */
    @NotBlank(message = "知识点名称不能为空")
    @Column(name = "name", nullable = false, length = 200)
    private String name;

    /**
     * 知识点描述
     */
    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    /**
     * 所属课程ID
     */
    @NotNull(message = "所属课程不能为空")
    @Column(name = "course_id", nullable = false)
    private Long courseId;

    /**
     * 所属章节ID
     */
    @NotNull(message = "所属章节不能为空")
    @Column(name = "chapter_id", nullable = false)
    private Long chapterId;

    /**
     * 基础分类（基础/重点/难点/拓展）
     * 与F-08基础数据管理模块的分类标准保持一致
     */
    @NotNull(message = "基础分类不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "basic_category", nullable = false, length = 20)
    private KnowledgePointCategoryEnum basicCategory;

    /**
     * 知识点状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    @Builder.Default
    private ChapterStatusEnum status = ChapterStatusEnum.ACTIVE;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 创建人
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 更新人
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    /**
     * 是否已删除
     */
    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    // ========== 关联关系 ==========

    /**
     * 所属课程
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "course_id", referencedColumnName = "id", insertable = false, updatable = false)
    private Course course;

    /**
     * 所属章节
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "chapter_id", referencedColumnName = "id", insertable = false, updatable = false)
    private CourseChapter chapter;

    // ========== JPA生命周期回调 ==========

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
        this.updatedTime = now;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updatedTime = LocalDateTime.now();
    }
}